
package com.zbs.framework.dao.security.config;

import com.zbs.framework.dao.permission.entity.Permission;
import com.zbs.framework.dao.permission.service.PermissionService;
import com.zbs.framework.security.filter.DynamicSecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * mall-security模块相关配置
 * 自定义配置，用于配置如何获取用户信息及动态权限
 * Created by macro on 2022/5/20.
 */
@Configuration
public class UrlResourceSecurityConfig {

    @Autowired
    private PermissionService permissionService;

    @Bean
    public DynamicSecurityService dynamicSecurityService() {
        return () -> {
            Map<String, ConfigAttribute> map = new ConcurrentHashMap<>();
            List<Permission> allPerms = this.permissionService.findAllPerms();
            allPerms.forEach(permission -> {
                map.put(permission.getPath(),new SecurityConfig(String.format("%s::%s",permission.getServiceName(),permission.getName())));
            });
            return map;
        };
    }
 
}